package com.usian.controller;

import com.usian.feign.ItemFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.PreUpdate;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/backend/item")
public class ItemController {

    @Autowired
    private ItemFeign itemFeign;

    @RequestMapping("/findItemById")
    public Result findItemById(@RequestParam Long id){
        TbItem itemById = itemFeign.findItemById(id);
        itemById.setPrice(itemById.getPrice());
        if (itemById==null){
            return Result.error("查无结果");
        }else {
            return Result.ok(itemById);
        }
    }

    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer rows){
        PageResult pageResult = itemFeign.selectTbItemAllByPage(page, rows);
        if (pageResult.getResult()!=null && pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    @RequestMapping("/insertTbItem")
    public Result insertTbItem(TbItem tbItem,
                               @RequestParam String desc, @RequestParam String itemParams){
        Boolean flag = itemFeign.insertTbItem(tbItem,desc,itemParams);
        if (flag){
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("deleteItemById")
    public Result deleteItemById(@RequestParam String itemId){

        Boolean flag = itemFeign.deleteItemById(itemId);
        if (flag){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @RequestMapping("preUpdateItem")
    public Result preUpdateItem(@RequestParam Long itemId){
        PreUpdate preUpdate = itemFeign.preUpdateItem(itemId);
        preUpdate.getItem().setPrice(preUpdate.getItem().getPrice()/100);
        if (preUpdate!=null){
            return Result.ok(preUpdate);
        }
        return Result.error("回显失败");
    }

    @RequestMapping("updateTbItem")
    public Result updateTbItem(TbItem tbItem,
                               @RequestParam String desc, @RequestParam String itemParams){
        Boolean flag = itemFeign.updateTbItem(tbItem,desc,itemParams);
        if (flag){
            return Result.ok();
        }
        return Result.error("添加失败");
    }

}
